import { FormSchema } from '/@/components/Form';
import { BasicColumn } from '/@/components/Table';
import { companyTreeData } from '/@/api/sys/company';

/**
 * 搜索表单配置
 * @param t 国际化函数
 * @param handleSuccess 成功回调函数
 * @returns FormSchema[]
 */
export const getSearchFormSchemas = (
  t: (key: string) => string,
  handleSuccess: (record: Recordable) => void,
  isShowBusType: boolean = true,
): FormSchema[] => [
  {
    label: t('公司'),
    field: 'companyCode',
    fieldLabel: 'company.companyName',
    component: 'TreeSelect',
    componentProps: {
      api: companyTreeData,
      allowClear: true,
      immediate: true,
      onChange: handleSuccess,
    },
  },
  {
    label: t('出库单号'),
    field: 'code_like',
    component: 'Input',
  },
  {
    label: t('拣货单号'),
    field: 'pickCode',
    component: 'Input',
  },
  {
    label: t('任务单号'),
    field: 'sourceId',
    component: 'Input',
  },
  {
    label: t('单据日期'),
    field: 'ddate',
    component: 'RangePicker',
  },
  {
    label: t('仓库'),
    field: 'basWare.cwhname',
    component: 'Input',
  },
  {
    label: t('业务类型'),
    field: 'busType',
    component: 'Select',
    componentProps: {
      dictType: 'wh_rd02_bus_type',
      allowClear: true,
    },
    ifShow: isShowBusType,
  },
  {
    label: t('红蓝标志'),
    field: 'bred',
    component: 'Select',
    componentProps: {
      dictType: 'wms_bred_status',
      allowClear: true,
    },
  },
  {
    label: t('状态'),
    field: 'status',
    component: 'Select',
    componentProps: {
      dictType: 'bpm_biz_status',
      allowClear: true,
      onChange: handleSuccess,
    },
  },
  {
    label: t('是否推送'),
    field: 'bsend',
    component: 'Select',
    componentProps: {
      dictType: 'sys_yes_no',
      allowClear: true,
    },
  },
  {
    label: t('备注'),
    field: 'remarks',
    component: 'Input',
  },
];

/**
 * 表格列配置
 * @param t 国际化函数
 * @returns BasicColumn[]
 */
export const getTableColumns = (t: (key: string) => string): BasicColumn[] => [
  {
    title: t('出库单号'),
    dataIndex: 'code',
    key: 'a.code',
    sorter: true,
    width: 120,
    align: 'left',
    slot: 'firstColumn',
  },
  {
    title: t('拣货单号'),
    dataIndex: 'pickCode',
    key: 'a.pick_code',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('任务单号'),
    dataIndex: 'sourceId',
    key: 'a.source_id',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('单据日期'),
    dataIndex: 'ddate',
    key: 'a.ddate',
    sorter: true,
    width: 100,
    align: 'left',
  },
  {
    title: t('仓库'),
    dataIndex: 'basWare.cwhname',
    key: 'a.wh_code',
    sorter: true,
    width: 100,
    align: 'left',
  },
  {
    title: t('业务类型'),
    dataIndex: 'busType',
    key: 'a.bus_type',
    sorter: true,
    width: 100,
    align: 'left',
    dictType: 'wh_rd02_bus_type',
  },
  {
    title: t('红蓝标志'),
    dataIndex: 'bred',
    key: 'a.bred',
    sorter: true,
    width: 90,
    align: 'center',
    dictType: 'wms_bred_status',
  },
  {
    title: t('公司'),
    dataIndex: 'company.companyName',
    key: 'a.company_code',
    sorter: true,
    width: 120,
    align: 'left',
  },
  {
    title: t('制单时间'),
    dataIndex: 'createDate',
    key: 'a.create_date',
    sorter: true,
    width: 100,
    align: 'center',
  },
  {
    title: t('制单人'),
    dataIndex: 'createByName',
    key: 'a.create_by_name',
    sorter: true,
    width: 90,
    align: 'left',
  },
  {
    title: t('客户'),
    dataIndex: 'cusCode',
    key: 'a.cus_code',
    sorter: true,
    width: 90,
    align: 'left',
    dictType: 'wms_cus',
  },
  {
    title: t('备注'),
    dataIndex: 'remarks',
    key: 'a.remarks',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('推送'),
    dataIndex: 'bsend',
    key: 'a.bsend',
    sorter: true,
    width: 100,
    align: 'center',
    dictType: 'sys_yes_no',
    fixed: 'right',
  },
  {
    title: t('状态'),
    dataIndex: 'status',
    key: 'a.status',
    sorter: true,
    width: 100,
    align: 'center',
    dictType: 'bpm_biz_status',
    fixed: 'right',
  },
];

/**
 * 操作列配置
 * @param t 国际化函数
 * @param handleForm 表单处理函数
 * @param handleDelete 删除处理函数
 * @param handleTrace 流程追踪函数
 * @returns BasicColumn
 */
export const getActionColumn = (
  t: (key: string) => string,
  handleForm: (record: Recordable) => void,
  handleDelete: (record: Recordable) => void,
  handleTrace: (record: Recordable) => void
): BasicColumn => ({
  width: 160,
  actions: (record: Recordable) => [
    {
      icon: 'i-clarity:note-edit-line',
      title: t('编辑出库主表'),
      onClick: handleForm.bind(null, { code: record.code }),
      auth: 'wh:rd02:rd02:edit',
      ifShow: () => record.status == '9',
    },
    {
      icon: 'i-ant-design:delete-outlined',
      color: 'error',
      title: t('删除出库主表'),
      popConfirm: {
        title: t('是否确认删除出库主表'),
        confirm: handleDelete.bind(null, record),
      },
      auth: 'wh:rd02:rd02:edit',
      ifShow: () => record.status == '9',
    },
    {
      icon: 'i-fluent:flowchart-20-regular',
      title: t('流程追踪'),
      onClick: handleTrace.bind(null, record),
      ifShow: () => record.status != '9',
    },
  ],
});

/**
 * 搜索表单的时间字段映射配置
 */
export const fieldMapToTime = [['ddate', ['ddate_gte', 'ddate_lte']]];
